package com.zhkj.txg.module.order.ui.group

import android.content.Context
import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.zhkj.lib.base.BaseActivity
import com.zhkj.txg.R
import com.zhkj.txg.module.order.adapter.OrderListTabAdapter
import kotlinx.android.synthetic.main.activity_order_list.*

class GroupOrderListActivity : BaseActivity() {
    companion object {
        private val INDEX = "index"
        fun open(context: Context, index: Int) {
            val intent = Intent(context, GroupOrderListActivity::class.java).putExtra(INDEX, index)
            context.startActivity(intent)
        }
    }

    override fun statusTitleBar(): View? = titleOrderList
    override val layoutResId: Int = R.layout.activity_order_list

    private val tabAdapter = OrderListTabAdapter().apply {
        setOnItemClickListener { _, _, position ->
            selectedPosition = position
            notifyDataSetChanged()
            vpOrderList.setCurrentItem(position, false)
        }
    }

    override fun initUI() {
        with(rvOrderListTab) {
            layoutManager = GridLayoutManager(this@GroupOrderListActivity, 4)
            adapter = tabAdapter
        }
        with(vpOrderList) {
            getChildAt(0)?.overScrollMode = View.OVER_SCROLL_NEVER
            adapter = object : FragmentStateAdapter(this@GroupOrderListActivity) {
                override fun getItemCount(): Int = 4
                override fun createFragment(position: Int) =
                    GroupOrderListFragment.newInstance(Type.values()[position].type)
            }
            registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
                override fun onPageSelected(position: Int) {
                    tabAdapter.selectedPosition = position
                }
            })
        }
    }

    override fun initData() {
        // 添加tab数据
        val list = mutableListOf(
            getString(R.string.all),
            getString(R.string.group_ing),
            getString(R.string.group_success),
            getString(R.string.group_fail)
        )
        tabAdapter.setNewData(list)
        // 显示当前Fragment
        val index = intent.getIntExtra(INDEX, 0)
        tabAdapter.selectedPosition = index
        tabAdapter.notifyDataSetChanged()
        vpOrderList.setCurrentItem(index, false)
    }
}

enum class Type(val type: String) {
    ALL("all"), PROCESSING("processing"), FINISH("finish"), FAIL("fail")
}